package com.itzhigui.controller;

import com.itzhigui.config.MyUserConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 模拟控制器在是否登录状态可以访问其中接口
 *
 * @author java1412
 * @since 2024-04-12 10:06:03
 */
@RestController
public class UserController {

    // 未登录，可以访问
    @PostMapping(value = "/login")
    public Object login(
            String username,
            HttpSession session
    ) {
        if ("admin".equals(username)) {
            session.setAttribute("loginUser", "admin");
            return "ok";
        } else {
            return "no";
        }
    }

    // 未登录不可以访问
    @GetMapping(value = "/getCurrentUser")
    public Object getCurrentUser() {
        System.out.println("admin");
        return "admin";
    }

    // 定义三个属性username,password,age，来接收配置文件username，password，age的值
//    @Value("${user.username}")
//    private String username;
//
//    @Value("${user.password}")
//    private String password;
//
//    @Value("${user.age}")
//    private Integer age;
//
//    @GetMapping(value = "/testValue")
//    public String testValue() {
//        String str = "用户名:" + this.username + ",密码:" + this.password + ",年龄:" + this.age;
//        return str;
//    }

    @Resource
    private MyUserConfig myUserConfig;

    @GetMapping(value = "/testValue2")
    public String testValue2() {
        String str = "用户名:" + myUserConfig.getUsername()
                + ",密码:" + myUserConfig.getPassword()
                + ",年龄:" + myUserConfig.getAge();
        return str;
    }
}
